package com.TPNexus.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import org.hibernate.Query;
import org.hibernate.Session;
import com.TPNexus.model.ShoppingCart;
import com.TPNexus.model.ShoppingCartItem;

@Named
@ApplicationScoped
public class ShoppingCartDAO {
	private Session session=HibernateInitial.getSession();
	public ArrayList<ShoppingCartItem> getShoppingCartItemList(long buyerId) {
		session.beginTransaction();
		ArrayList<ShoppingCartItem> list = new ArrayList<ShoppingCartItem>();
		Query query = session
				.createQuery("select sc.shoppingCartItems from ShoppingCart sc  where sc.userId="
						+ buyerId);
		list = (ArrayList<ShoppingCartItem>) query.list();

		session.getTransaction().commit();
		return list;
	}

	public ShoppingCart getShoppingCartbyUser(long buyerId) {
		session.beginTransaction();
		ShoppingCart result = null;
		result=(ShoppingCart) session.createQuery("From ShoppingCart sc where sc.userId="+buyerId).uniqueResult();

		session.getTransaction().commit();
//		session.close();
		return result;
	}

	public int creatNewShoppingCart(ShoppingCart shoppingCart) {
		int result;
		try {
			session.beginTransaction();
			session.save(shoppingCart);
			session.getTransaction().commit();
			result = 1;
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			result = 0;
			throw e;
		}
		return result;
	}

	public int addToShoppingCart(ShoppingCartItem shoppingCartItem,
			ShoppingCart shoppingCart) {
		int resultx;
		boolean result = true;
		try {
			session.beginTransaction();
			for (ShoppingCartItem CartItem : shoppingCart
					.getShoppingCartItems()) {
				if (CartItem.getCatalogItem().getCatalogItemId() == shoppingCartItem
						.getCatalogItem().getCatalogItemId()) {
					CartItem.setQty(CartItem.getQty()
							+ shoppingCartItem.getQty());
					result = false;
				}
			}
			if (result) {
				shoppingCart.getShoppingCartItems().add(shoppingCartItem);
			}
			session.merge(shoppingCart);
			session.getTransaction().commit();
			resultx = 1;
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			resultx = 0;
			throw e;
		}
		return resultx;
	}

	public boolean deleteShoppingCartItem(ShoppingCartItem shoppingCartItem,
			ShoppingCart shoppingCart) {
		boolean result = false;
		try {
			session.beginTransaction();
			if (shoppingCart.getShoppingCartItems().contains(shoppingCartItem)) {
				shoppingCart.getShoppingCartItems().remove(shoppingCartItem);

				result = true;
			}
			session.update(shoppingCart);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			result = false;
			throw e;
		}
		return result;
	}

	public int deleteShoppingCart(ShoppingCart shoppingCart) {
		int result;
		try {
			session.beginTransaction();
			ShoppingCart getshoppingCart=(ShoppingCart) session.get(ShoppingCart.class, shoppingCart.getShoppingCartId());
			session.delete(getshoppingCart);
			session.getTransaction().commit();
			result = 1;
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			result = 0;
			throw e;
		}
		return result;
	}

	public int updateShoppingCartItemQty(ShoppingCartItem shoppingCartItem) {// is
																				// it
																				// better
																				// to
																				// change
																				// to
																				// qty:int?
		int result;
		try {
			session.beginTransaction();

			session.update(shoppingCartItem);
			session.getTransaction().commit();
			result = 1;
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			result = 0;
			throw e;
		}
		return result;
	}

	public int updateShoppingCart(ShoppingCart shoppingCart) {
		int result;
		try {
			session.beginTransaction();

			session.merge(shoppingCart);
			session.getTransaction().commit();
			result = 1;
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			System.out.println(e.toString());
			result = 0;
			throw e;
		}
		return result;
	}
}
